import
ipConfig
from './config.js'
import {
 JSEncrypt
} from "jsencrypt";



// function encryptedData(publicKey, data) { // 新建JSEncrypt对象 
// 	let encryptor = new JSEncrypt(); // 设置公钥 00
// 	encryptor.setPublicKey(publicKey); // 加密数据 
// 	return encryptor.encrypt(data);
// }
/**
 * file:data 数据模式 file 文件模式
 */
function request(url, method = 'get', data, serviceName = '', contentType = 1, port, file = 'data') {
	//线上环境
	// if(!port) port = '8091'
	// let portNum = serviceName == 'login' ? '8093' : port
	//测试环境
	if (!port) port = '8082';
	console.log('serviceName',serviceName)
	let portNum = serviceName == 'login' ? '8083' : port;
	if(portNum == '8082'){
		portNum = '9988'
	}else if(portNum == '8083'){
		portNum = '9988/auth2'
	}
	//外网环境
	// if(!port) port = '38081'
	// if(port == '8085') port = '38085';
	// if(port == '8082') port = '9988/yuezhudaiketong';
	// let portNum = serviceName == 'login' ? '38083' : port

	let contentTypeStr = contentType == 1 ? 'application/json' : 'application/x-www-form-urlencoded'
	let portNumUrl = `:${portNum}${url}`
	let publickKey =
		'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCOEXvidKC7bIqIdnaTUXfFG/S0yFCbXywGqGH8ZTqfA59GyZK5YXddT0bNxXorGwiftoWaLJUctJjEk4yeiCc/ljsJyLibKW7wjVbxYW/m7Lv+eIwxFLBHAV8xkA3G1cW4SxRbhjPUU2d9t/byvoGxlR64MVD8jkRK3NXmVXZorQIDAQAB'
	let loadingTime = setTimeout(() => {
		uni.showToast({
			icon: 'loading',
			title: '加载中...',
			duration: 5000
		})
	}, 500)
	uni.setStorageSync('commonToken', "bcf27420-c90a-46ed-8ad0-3353e80a7246")
	let commonTokonStr = uni.getStorageSync('commonToken')
	// let token = commonTokonStr ? encryptedData(publickKey, commonTokonStr) : ''
	let token = commonTokonStr;
		// console.log('文件模式',file)
	return new Promise((res, rej) => {
		if (file === 'data') {
			uni.request({
				url: `${ipConfig.baseUrl}${portNumUrl}`,
				method: method,
				data: data,
				header: {
					'Content-Type': contentTypeStr,
					'access_token': token,
				},
				dataType: 'json',
				timeout: 5000,
				success: (data) => {
					clearTimeout(loadingTime)
					uni.hideToast()
					let obj = data.data
					if (obj.code) {
						obj.result = obj.code
					}
					if (obj.result != 1) {
						uni.showToast({
							icon: 'none',
							title: obj.message,
							duration: 2000
						});
						if (obj.result == 20001) {
							setTimeout(() => {
								uni.redirectTo({
									url: '/pages/login/login',
									fail: (re) => {
										console.log(re)
									}
								})
							}, 2000)

						}
					} else {
						res(data)
					}
				},
				fail: (error) => {
					clearTimeout(loadingTime)
					uni.showToast({
						icon: 'none',
						title: error.message,
						duration: 2000
					});
					rej(error)
				}
			});
		} else {			
			// const urlstr = data.file.substring(data.file.lastIndexOf('/') + 1);
			console.log('data.file',data)
			var formData = {};
			formData['file'] = data.file;
			//上传图片在浏览器测试会提示报错，但在app中上传图片一切正常，无需修改
			uni.uploadFile({
				url: `${ipConfig.baseUrl}${portNumUrl}`,
				formData: formData,
				header:{
					'ACCESS_TOKEN':token
				},
				success: (data) => {
					clearTimeout(loadingTime);
					uni.hideToast()
					let obj = JSON.parse(data.data);
					if (obj.result != 1) {
						uni.showToast({
							icon: 'none',
							title: obj.message,
							duration: 2000
						});
						if(obj.result==20001){
							setTimeout(()=>{
								uni.redirectTo({
									url:'/pages/login/login',
									fail:(re)=>{
										console.log(re)
									}
								})
							},2000)
							
						}
					} else {					
						res(obj)
					}
				},
				fail: (error) => {
					uni.showToast({
						title:JSON.stringify(error),
						duration:5000
						
					})
					uni.showToast({
						title:'提交失败！！！'
					})
					clearTimeout(loadingTime)
					uni.showToast({
						icon: 'none',
						title: error.message,
						duration: 2000
					});
					rej(error)
				}
			})
		}

	})
}

export default request
